<?php
//function __autoload($classname){require(SITE_DIR."/core/wizard/".$classname.".class.php");}
/**
 * @package SCS
 * @subpackage Category
 * @author Igor.Barkowsky@gmail.com
 * @copyright 2008
 */

class CategoryWizard extends Wizard
{
	var $table = 'category';
	var $pfx = '';
	var $pid = 'category_id';
	var $iname='id';
	var $pname='pid';
	var $padding=3;
	var $padding_element='&nbsp;';
	/*
	function __construct(MyDB $db,$tablename = '',$pfx = '',$pid = '')
	{
		$this->db = $db;
		if(strlen($tablename) > 0){$this->table = mysql_real_escape_string($tablename);}
		if(strlen($pfx) > 0){$this->pfx = mysql_real_escape_string($pfx);}
		if(strlen($id) > 0){$this->pid = $id;}
	}
	*/
	function byAlias($alias,$module = '')
	{
		if($alias != '')
		{
			if($module == '')
			{
				$res = $this->db->fetch_one("SELECT * FROM $this->table WHERE alias = '".mysql_real_escape_string($alias)."'");
			}
			else
			{
				$res = $this->db->fetch_one("SELECT * FROM $this->table WHERE alias = '".mysql_real_escape_string($alias)."' AND module = '".mysql_real_escape_string($module)."'");
			}
			if(!empty($res))
			{
				return $res;
			}
			else return false;
		}
		else return false;
	}
	function html_options($parent = 0, $selected = 0, $status=2, $level_limiter = 0, $level=0)
	{
		static $selected,$level_limiter;
		$selected = intval($selected);
		$level_limiter = intval($level_limiter);
		$status = intval($status);
		$parent = intval($parent);
		$level = intval($level);
		$str = '';
		$query = "
		SELECT 
		$this->iname,$this->pname,title 
		FROM $this->table 
		WHERE $this->pname = '$parent' AND $this->iname<>'0' AND status>='$status' 
		ORDER BY order_num";
		$sql = $this->db->fetch_array($query,MYSQL_ASSOC);
		if ($sql!==false)
		{
			foreach($sql as $k=>$row)
			{	
				$str .= '<option value="'.$row[$this->iname].'"'.((intval($selected)==$row[$this->iname])?' selected':'').'>'.(($level>0)?str_repeat($this->padding_element, $level*$this->padding):'').$row['title'].'</option>';
	    		$next_level = $level + 1;
	    		if($level_limiter==0 or $level_limiter>=$next_level)
					$str .= $this->html_options($row[$this->iname],$selected,$status,$level_limiter,$next_level);
			}
		}
		return $str;
	}
	function recurse($parent = 0, $selected = 0, $status=2, $level_limiter = 0, $level=0)
	{
		static $selected,$level_limiter;
		$selected = intval($selected);
		$level_limiter = intval($level_limiter);
		$status = intval($status);
		$parent = intval($parent);
		$level = intval($level);
		$pages = array();
		$inside_selected=false;
		/** Query for category non-top level **/
		$query = "
		SELECT 
		$this->iname,$this->pname,title,alias,(SELECT count(*) WHERE $this->pname=$this->iname) AS childs 
		FROM $this->table 
		WHERE $this->pname = '$parent' AND $this->iname<>'0' AND status>='$status' 
		ORDER BY order_num";
		$rows = $this->db->fetch_array($query);
		$i = 0;
		foreach ($rows as $k=>$row)
		{
			$pages[$i][$this->iname] = $row[$this->iname];
			if (is_array($selected))
			{
	            if (in_array($row[$this->iname], $selected))
	    		{
	    			$pages[$i]['selected'] = 'selected';
	    			$inside_selected=true;
	    		}
	        }
	        else
	        {
	    		if (intval($selected) == $row[$this->iname])
	    		{
	    			$pages[$i]['selected'] = 'selected';
	    			$inside_selected=true;
	    		}
	    	}
			$pages[$i]['title'] = $row['title'];
			$pages[$i]['level'] = $level;
			$pages[$i]['alias'] = $row['alias'];
			$pages[$i]['childs']=intval($row['childs']);
	
			$next_level = $level + 1;
			if($level_limiter==0 or $next_level<=$level_limiter)
			{
				if ($pages[$i]['childs']>0)
				{
		            $sub = $this->recurse($row[$this->iname], $selected, $status,$level_limiter, $next_level);
					$pages[$i]['sub']=$sub['pages'];
					if($sub['inside_selected']===true)
					{
						$pages[$i]['inside_selected']=true;
						$inside_selected=true;
					}
				}
			}
			$i++;
		}
		return array('pages'=>$pages,'inside_selected'=>$inside_selected);
	}
	function getCatArray($array_of_cat_ids = array(),$status = 0)
	{
		if(is_array($array_of_cat_ids) and !empty($array_of_cat_ids))
		{
			foreach($array_of_cat_ids as $k=>$v)
			{
				$array_of_cat_ids[$k] = mysql_real_escape_string($v);
			}
			$sql_in = "('".implode("','",$array_of_cat_ids)."')";
			$categoriesArray = $this->db->fetch_array("SELECT DISTINCT * FROM $this->table WHERE id IN $sql_in AND status >= '$status'");
			if(!empty($categoriesArray))
			{
				return $categoriesArray;
			}
			else return false;
		}
		else return false;
	}
	/**
	 * CategoryWizard::makeAlias()
	 * 
	 * @param string $title
	 * @param string $module
	 * @param integer $parent
	 * @return string || false on error
	 */
	function makeAlias($title = '',$module = 'content',$parent = -1)
	{
		if(isset($module) and $module != '')
		{
			$sql_where[] = "module = '".mysql_real_escape_string($module)."'";
		}
		if(isset($parent) and $parent != -1)
		{
			$sql_where[] = "parent = '".mysql_real_escape_string($parent)."'";
		}
		if(isset($sql_where) and !empty($sql_where))
		{
			$where_clause = " AND ".implode(" AND ",$sql_where)." ";
		}
		else $where_clause = "";

	    $title = strtolower(trim($title));
	
	    $alias = $title;
	    if (strlen($title) > 0)
	    {
	
	        $eng = Array ('a', 'b', 'v', 'g', 'd', 'e', 'e', 'j', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'i', '', '', 'e', 'u', 'ya','a', 'b', 'v', 'g', 'd', 'e', 'e', 'j', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'i', '', '', 'e', 'u', 'ya');
	        $rus = Array ('а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ы', 'ъ', 'ь', 'э', 'ю', 'я','А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ы', 'Ъ', 'Ь', 'Э', 'Ю', 'Я');
	
	
	        foreach ($rus as $k=>$v)
	        {
	            $alias = str_replace($v, $eng[$k], $alias);
	        }
	
	        $alias = str_replace(" ", "_", $alias);
	        $alias = preg_replace("/\W+/","", $alias);
	        $alias = str_replace("___", "_", $alias);
	        $alias = substr(str_replace("__", "_", $alias),0, 95);
	        /** START 	check if is current alias exsists (by module type) **/
	        $is_exists = $this->db->fetch_one("SELECT count(*) FROM $this->table WHERE alias = '".mysql_real_escape_string($alias)."' $where_clause");
	        if ($is_exists[0] > 0)
	        {
	            $i=1;$fin = false;
	            while ($fin === false)
	            {
	                $check_alias = $alias."_".$i;
	                $is_exists = $this->db->fetch_one("SELECT count(*) FROM $this->table WHERE alias = '".mysql_real_escape_string($check_alias)."' $where_clause");
	                if ($is_exists[0] > 0)
	                {
	                    $i++;
	                }
	                else
	                {
	                    $alias = $check_alias;
	                    $fin = true;
	                }
	            }
	        }
	        return $alias;
	        /** END 	check if is current alias exsists (by module type) **/
	    }
	    else
	    {
	        return false;
	    }
	}
	function getParents($obj_id = 0)
	{
        $path = array();
        $item = $this->db->fetch_one("SELECT $this->iname, $this->pname, alias, title FROM $this->table WHERE $this->iname = '$obj_id' AND $this->pname = $this->iname");
        if ($item != false and !empty($item)){$path[] = $item;}
        if($item != false and !empty($item) && intval($item[$this->pname]) != 0)
        {
            $path = array_merge($this->getParents($page[$this->pname]), $path);
        }
        return $path;
	}
	function getChilds($obj_id = 0,$status = 0)
	{
		return $this->db->fetch_array("SELECT * FROM $this->table WHERE $this->pname = '".intval($obj_id)."' AND status >= '".intval($status)."'");
	}
}
?>